import _ from "lodash";
import { CompanyModules } from "../config";

export class AccessControl {
  modules = [];

  // Cache module item's id
  moduleItemIdList = [];

  /**
   * @param {string[]} acl
   */
  computedModules(acl) {
    const modules = _.cloneDeep(CompanyModules);

    // Omit group items
    modules.forEach(mo => {
      mo.items = mo.items.reduce((res, item) => {
        if (acl.includes(item.id)) {
          res.push(item);
          this.moduleItemIdList.push(item.id);
        }
        return res;
      }, []);
    });

    // Omit empty group
    this.modules = modules.filter(m => m.items.length);
  }

  has(id) {
    return this.moduleItemIdList.includes(id);
  }
}
